'use strict'

angular.module('mainApp')
.controller('jobDetailCtrl',function($scope, $state, $stateParams, tjzHttp, constant, ngDialog){
    var jobId = $stateParams.jobId;
    var init = function(){
        tjzHttp.postRequest(constant.url+ '/jobinfo',{jobId:jobId}).then(function(res){
            if(res.data.code == 0){
                $scope.info = res.data.data;
                if($scope.info.imageUrl){
                    var tmp = $scope.info.imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                    $scope.info.imageUrl = tmp;
                }
                $scope.info.date = $scope.info.date.split('.0')[0];
                for(var i=0;i<$scope.info.evaluateList.length;i++){
                    $scope.info.evaluateList[i].date = $scope.info.evaluateList[i].date.split('.0')[0];
                }
            }
        });
        tjzHttp.postRequest(constant.url + '/updateNum', {jobId:jobId}).then(function(){
        })
        if(window.localStorage.token){
            tjzHttp.postRequest(constant.url + '/create/history', {jobId:jobId}).then(function(){
            })
        }

    }
    $scope.favoriteJob = function(isDelete,needNum){
        if(isDelete){
            ngDialog.open({
                template: 'template/dialog.html',
                width: 600,
                controller: ['$scope', function ($scope) {
                    $scope.dialogTitle = '淘家政提示';
                    $scope.dialogContent = '该需求已下架，不能收藏';
                    $scope.yes = function () {
                        $scope.closeThisDialog();
                    }
                }]
            });
            return;
        }
        if(needNum == 0){
            ngDialog.open({
                template: 'template/dialog.html',
                width: 600,
                controller: ['$scope', function ($scope) {
                    $scope.dialogTitle = '淘家政提示';
                    $scope.dialogContent = '该需求人数已满，不支持收藏';
                    $scope.yes = function () {
                        $scope.closeThisDialog();
                    }
                }]
            });
            return;
        }
        var url = constant.url + '/create/favorite/job';
        tjzHttp.postRequest(url, {jobId: jobId}).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '收藏成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
    $scope.buy = function(isDelete,needNum){
        if(isDelete){
            ngDialog.open({
                template: 'template/dialog.html',
                width: 600,
                controller: ['$scope', function ($scope) {
                    $scope.dialogTitle = '淘家政提示';
                    $scope.dialogContent = '该需求已下架，不支持下单';
                    $scope.yes = function () {
                        $scope.closeThisDialog();
                    }
                }]
            });
            return;
        }
        if(needNum == 0){
            ngDialog.open({
                template: 'template/dialog.html',
                width: 600,
                controller: ['$scope', function ($scope) {
                    $scope.dialogTitle = '淘家政提示';
                    $scope.dialogContent = '该需求人数已满，不支持下单';
                    $scope.yes = function () {
                        $scope.closeThisDialog();
                    }
                }]
            });
            return;
        }
        if(window.localStorage.token){
            $state.go('submitOrder',{jobId:jobId});
        }else{
            ngDialog.open({
                template: 'template/dialog.html',
                width: 600,
                controller: ['$scope', function ($scope) {
                    $scope.dialogTitle = '淘家政提示';
                    $scope.dialogContent = '您尚未登录，不能进行下单';
                    $scope.yes = function () {
                        $scope.closeThisDialog();
                    }
                }]
            }).closePromise.then(function(){
                $state.go('login');
            });
        }
    }
    init();
})